package com.tz.crm.advice;

import com.alibaba.fastjson.JSON;
import com.tz.crm.dao.NormalLogDao;
import com.tz.crm.entity.NormalLog;
import com.tz.crm.entity.User;
import com.tz.crm.utils.HttpSessionUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.support.BindingAwareModelMap;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * NormalLogAdvice
 */
@Component
@Aspect
public class NormalLogAdvice {

    @Autowired
    private NormalLogDao normalLogDao;

    /**
     * normalLog
     *
     * @param jp JoinPoint
     */
//    @AfterReturning("execution(* com.tz.crm.controller.**.*Controller.*(..))")
    @Before("execution(* com.tz.crm.controller.**.*Controller.*(..))")
    public void normalLog(JoinPoint jp) {
        String className = jp.getTarget().getClass().getName();
        String methodName = jp.getSignature().getName();
        NormalLog normalLog = new NormalLog(className, methodName);

        //用户访问的 url 路径 (实际对应资源的value)
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes) ra;
        HttpServletRequest request = sra.getRequest();
        String url = request.getServletPath().toString();
        normalLog.setUrl(url);

        // 操作信息量 (一些对象的json数据)
        Object[] obj = jp.getArgs();
        StringBuilder builder = new StringBuilder();
        for (Object o : obj) {
            if (validInfo(o)) {
                builder.append(JSON.toJSONString(o));
                builder.append(";");
            }
        }
        normalLog.setInfo(builder.toString());

        // 用户姓名
        User user = HttpSessionUtil.getLoginUser(request.getSession());
        if (user != null) {
            normalLog.setUserRealName(user.getRealName());
        }

        //请求类型
        normalLog.setMethodType(request.getMethod());
        normalLogDao.save(normalLog);
    }

    /**
     * 去除无需保存的数据类型
     *
     * @param arg Object
     * @return boolean
     */
    private boolean validInfo(Object arg) {
        if (arg == null) {
            return false;
        }
        if (arg instanceof BindingAwareModelMap
                || arg instanceof HttpServletRequest
                || arg instanceof HttpServletResponse
                || arg instanceof HttpSession
                || arg instanceof BeanPropertyBindingResult
                || arg instanceof CommonsMultipartFile
                || arg instanceof DefaultMultipartHttpServletRequest
                || "RequestFacade".equals(arg.getClass().getSimpleName())
                || "StandardSessionFacade".equals(arg.getClass().getSimpleName())) {
            return false;
        } else {
            return true;
        }
    }

}
